package com.ruoyi.waterDistribution.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.business.waterDistribution.WiCropB;
import com.ruoyi.common.core.domain.business.waterDistribution.WiCropY;
import com.ruoyi.common.core.domain.business.waterDistribution.WiPeriodY;
import com.ruoyi.common.core.utils.zeroFill.ZeroFillUtils;
import com.ruoyi.waterDistribution.domain.WiPeriodYDynamicItem;
import com.ruoyi.waterDistribution.mapper.WiCropBMapper;
import com.ruoyi.waterDistribution.mapper.WiCropYMapper;
import com.ruoyi.waterDistribution.mapper.WiPeriodYMapper;
import com.ruoyi.waterDistribution.service.IWiPeriodYService;
import com.ruoyi.waterDistribution.vo.WiCropYVo;
import com.ruoyi.waterDistribution.vo.WiPeriodYObject;
import com.ruoyi.waterDistribution.vo.WiPeriodYVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 年度作物灌溉周期Service业务层处理
 *
 * @author ps
 * @date 2023-02-16
 */
@Service
public class WiPeriodYServiceImpl implements IWiPeriodYService {
    @Autowired
    private WiPeriodYMapper wiPeriodYMapper;
    @Autowired
    private WiCropYMapper wiCropYMapper;
    @Autowired
    private WiCropBMapper wiCropBMapper;

    /**
     * 查询年度作物灌溉周期
     *
     * @param wiPeriodY 年度作物灌溉周期主键
     * @return 年度作物灌溉周期
     */
    @Override
    public Object selectWiPeriodYByCropYr(WiPeriodY wiPeriodY) {
        //按规则整体 数据 {cropYr plantCd dynamicItem}
        WiPeriodYObject wiPeriodYObject = new WiPeriodYObject();
        List<WiPeriodYVo> wiPeriodYVos = wiPeriodYMapper.selectWiPeriodYList(wiPeriodY);
        if (wiPeriodYVos != null) {
            wiPeriodYObject.setCropYr(wiPeriodYVos.get(0).getCropYr());
            wiPeriodYObject.setPlantCd(wiPeriodYVos.get(0).getPlantCd());
            wiPeriodYObject.setDynamicItem(wiPeriodYVos);
        }
        return wiPeriodYObject;
    }

    /**
     * 查询年度作物灌溉周期列表
     *
     * @param wiPeriodY 年度作物灌溉周期
     * @return 年度作物灌溉周期
     */
    @Override
    public List<WiPeriodYVo> selectWiPeriodYList(WiPeriodY wiPeriodY) {
        List<WiPeriodYVo> wiPeriodYVos = wiPeriodYMapper.selectWiPeriodYList(wiPeriodY);
        for (WiPeriodYVo vo : wiPeriodYVos) {
            //先找到年度作物的作物id
            WiCropY wiCropY = new WiCropY();
            wiCropY.setCropYr(vo.getCropYr());
            wiCropY.setPlantCd(vo.getPlantCd());
            WiCropYVo wiCropYVo = wiCropYMapper.selectWiCropYByCropYrAndPlantCd(wiCropY);
            if (wiCropYVo != null) {
                WiCropB wiCropB = wiCropBMapper.selectWiCropBByCropCd(wiCropYVo.getCropCd());
                if (wiCropB != null) {
                    vo.setCropNm(wiCropB.getCropNm());
                }
            }
        }
        return wiPeriodYVos;
    }

    /**
     * 新增年度作物灌溉周期
     *
     * @param wiPeriodYVo 年度作物灌溉周期
     * @return 结果
     */
    @Override
    public int insertWiPeriodY(WiPeriodYVo wiPeriodYVo) {
        List<WiPeriodYDynamicItem> dynamicItem = wiPeriodYVo.getDynamicItem();
        wiPeriodYVo.setCropYr(wiPeriodYVo.getCropYr());
        wiPeriodYVo.setPlantCd(wiPeriodYVo.getCropCd());
        for (WiPeriodYDynamicItem item : dynamicItem) {
            WiPeriodY wiPeriodY = new WiPeriodY();
            wiPeriodY.setCropYr(wiPeriodYVo.getCropYr());
            wiPeriodY.setPlantCd(wiPeriodYVo.getCropCd());
            List<WiPeriodYVo> wiPeriodYs = wiPeriodYMapper.selectWiPeriodYList(wiPeriodY);

            wiPeriodYVo.setPeriodNum(Long.valueOf((wiPeriodYs.size() + 1) + ""));
            wiPeriodYVo.setBeginDate(item.getBeginDate());
            wiPeriodYVo.setEndDate(item.getEndDate());
            wiPeriodYVo.setPeriodVol(item.getPeriodVol());
            wiPeriodYMapper.insertWiPeriodY(wiPeriodYVo);
        }
        return 1;
    }

    /**
     * 修改年度作物灌溉周期
     *
     * @param wiPeriodYVo 年度作物灌溉周期
     * @return 结果
     */
    @Override
    public int updateWiPeriodY(WiPeriodYVo wiPeriodYVo) {
        int i = wiPeriodYMapper.deleteWiPeriodYByCropYr(wiPeriodYVo);
        if (i > 1) {
            this.insertWiPeriodY(wiPeriodYVo);
        } else {
            return 0;
        }
        return 1;
    }

    /**
     * 批量删除年度作物灌溉周期
     *
     * @param cropYrs 需要删除的年度作物灌溉周期主键
     * @return 结果
     */
    @Override
    public int deleteWiPeriodYByCropYrs(String[] cropYrs) {
        return wiPeriodYMapper.deleteWiPeriodYByCropYrs(cropYrs);
    }

    /**
     * 删除年度作物灌溉周期信息
     *
     * @param wiPeriodY 年度作物灌溉周期主键
     * @return 结果
     */
    @Override
    public int deleteWiPeriodYByCropYr(WiPeriodY wiPeriodY) {
        return wiPeriodYMapper.deleteWiPeriodYByCropYr(wiPeriodY);
    }
}
